package com.yixun.lbx.controller.farm;

import com.yixun.controller.BaseController;
import com.yixun.lbx.entity.YxProBeeSwarm;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.service.BeeSwarmService;
import com.yixun.lbx.service.CompanyService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/bs")
public class BeeSwarmController extends BaseController {
    private final BeeSwarmService beeSwarmService;
    private final CompanyService companyService;

    public BeeSwarmController(CompanyService companyService, BeeSwarmService beeSwarmService) {
        this.companyService = companyService;
        this.beeSwarmService = beeSwarmService;
    }

    /*
     *获取蜂群列表
     */
    @PostMapping("/getListByPage")
    public HashMap<String, Object> getListByPage(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        Integer count = beeSwarmService.getCount(map);
        List<YxProBeeSwarm> beeSwarms = beeSwarmService.getListByPage(map);

        map.put("count", count);
        map.put("beeSwarms", beeSwarms);
        return map;
    }

    /*
     *添加蜂群
     */
    @PostMapping("/addBeeSwarm")
    public int addBeeHive(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        map.put("addUser", this.getSessionUser().getUser_id());
        return beeSwarmService.addBeeSwarm(map);
    }

    /*
     *修改蜂群
     */
    @PostMapping("/updateBeeSwarm")
    public int updateBeeHive(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] beeIds = map.get("beeId").toString().split(",");
        map.put("beeIds", beeIds);
        return beeSwarmService.updateBeeSwarm(map);
    }

    /*
     *淘汰蜂群
     */
    @PostMapping("/deleteBeeSwarm")
    public int deleteBeeHive(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] beeIds = map.get("beeId").toString().split(",");
        map.put("beeIds", beeIds);
        return beeSwarmService.deleteBeeSwarm(map);
    }

    @GetMapping("/getObjByArgs")
    public List<YxProBeeSwarm> getObjByArgs(YxProBeeSwarm beeSwarm) {
        beeSwarm.setQyId( String.valueOf(this.getQyId()));
        return beeSwarmService.getObjByArgs(beeSwarm);
    }

    /*搜索*/
    @RequestMapping("/getSearchListByPage")
    public HashMap<String, Object> getSearchListByPage(@RequestBody HashMap<String, String> map) {
        map.put("qyId", String.valueOf(this.getQyId()));
        List<YxProBeeSwarm> obj = beeSwarmService.getSearchListByPage(map);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("obj", obj);
        hashMap.put("count", obj.size());
        return hashMap;
    }

    private Long getQyId() {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(this.getSessionUser().getUser_name());
        YxProCompany company = companyService.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            return company.getQyId();
        } else {
            return 0L;
        }
    }
}
